package com.blog.backend.service.impl;

import com.blog.backend.mapper.BlogFileMapper;
import com.blog.backend.model.BlogFile;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.service.FileService;
import com.blog.backend.utils.FileUploadUtil;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.FileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author fanggs
 * @Date 2021/7/16 0016 11:39
 */

@Slf4j
@Service
public class FileServiceImpl implements FileService {

    public static final String BUCKET_NAME="dev";

    @Resource
    private BlogFileMapper blogFileMapper;

    @Autowired
   private FileUploadUtil fileUploadUtil;

    @Override
    public PictureUrlResp uploadFile(MultipartFile file,Integer type) {
        log.info("上传图片文件");
        FileVo fileVo = fileUploadUtil.uploadFile(file);
        BlogFile blogFile = new BlogFile();
        String id = UuidUtils.getId();
        blogFile.setFileId(id);
        blogFile.setFileUrl(fileVo.getFileUrl());
        blogFile.setFileName(fileVo.getFileName());
        blogFile.setRealPath(fileVo.getRealPath());
        blogFile.setType(type);
        blogFileMapper.insertSelective(blogFile);
        //返回给前端需要的数据
        PictureUrlResp resp = new PictureUrlResp();
        resp.setPictureId(id);
        resp.setPictureUrl(fileVo.getFileUrl());
        return resp;
    }

    @Override
    public Boolean deleteFile(String fileId) {
        blogFileMapper.deleteFile(fileId);
        return true;
    }
}
